const crypto = require('crypto')
const password = 'CYbr3GXthuN8DzWU67RjbXbg4LSTgAYj' // 密码，客户端和服务端共用

export function decrypt(encryptedData) {
  const d = JSON.parse(encryptedData)
  const decipher = crypto.createDecipheriv(
    'aes-256-cbc',
    Buffer.from(password),
    Buffer.from(d.iv, 'hex')
  )
  let decrypted = decipher.update(d.data, 'hex', 'utf-8')
  decrypted += decipher.final('utf-8')
  return decrypted
}

// import CryptoJS from 'crypto-js'
// const password = 'CYbr3GXthuN8DzWU67RjbXbg4LSTgAYj' // 密码，客户端和服务端共用

// export function decrypt(encryptedData) {
//   const d = JSON.parse(encryptedData)
//   // 将密钥和 IV 转换为 WordArray
//   let keyWA = CryptoJS.enc.Utf8.parse(d.data)
//   let ivWA = CryptoJS.enc.Utf8.parse(d.iv)
//   // 解密
//   let decrypted = CryptoJS.AES.decrypt(password, keyWA, {
//     iv: ivWA,
//     mode: CryptoJS.mode.CBC,
//     padding: CryptoJS.pad.Pkcs7
//   })

//   // 将解密后的数据转换为 UTF8 字符串
//   let decryptedData = decrypted.toString(CryptoJS.enc.Utf8)

//   console.log(decryptedData)
//   debugger
// }
